<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>用户登录</h2>
        <p>请输入用户名和密码登录系统</p>
      </div>

      <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="rules"
          class="login-form"
      >
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              class="input-field"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              class="input-field"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              @click="handleLogin"
              class="login-btn"
              :loading="loading"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            // 适配后端表单参数格式
            const params = new URLSearchParams()
            params.append('username', this.loginForm.username)
            params.append('password', this.loginForm.password)

            const response = await this.$http.post('/findUser', params)

            // 后端直接返回Result对象，无需.data嵌套
            if (response.code === 200) {
              // 保存token和用户信息
              localStorage.setItem('token', response.data.token)
              localStorage.setItem('user', JSON.stringify(response.data.user))
              this.$message.success('登录成功')
              this.$router.push('/home')
            } else {
              this.$message.error(response.msg || '登录失败')
            }
          } catch (error) {
            this.$message.error('网络错误，请稍后重试')
            console.error(error)
          } finally {
            this.loading = false
          }
        }
      })
    }
  },
  mounted() {
    // 如果已登录，跳转到首页
    if (localStorage.getItem('token')) {
      this.$router.push('/home')
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #4AB1FF 0%, #0066FF 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  animation: slideUp 0.5s ease-out;
}

.login-header {
  padding: 30px 20px;
  text-align: center;
  background-color: #f8f9fa;
  border-bottom: 1px solid #eee;
}

.login-header h2 {
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form {
  padding: 30px 20px;
}

.input-field {
  width: 100%;
  height: 45px;
  border-radius: 25px;
}

.login-btn {
  width: 100%;
  height: 45px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(48, 126, 255, 0.4);
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>